package cn.unknowpupil.init.controller.monitor;


import cn.unknowpupil.init.common.enums.BusinessType;
import cn.unknowpupil.init.common.resopnse.ResultResponse;
import cn.unknowpupil.init.common.service.TokenService;
import cn.unknowpupil.init.components.annotation.Log;
import cn.unknowpupil.init.domain.dto.SysUserOnline;
import cn.unknowpupil.init.service.system.IUserOnlineService;

import cn.unknowpupil.init.utils.QiniuUtils;
import com.baomidou.mybatisplus.core.metadata.IPage;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;


/**
 * Created at 2020/10/22 by w10g <br>
 * Initialize MonitorController ... <br>
 *
 * @author unknowpupil
 * @RequiredArgsConstructor： 会生成一个包含常量，和标识了NotNull的变量的构造方法。生成的构造方法是私有的private。
 * @since 0.0.1
 */
@RestController

@RequiredArgsConstructor
@Api(tags = "系统-服务监控管理")
@RequestMapping("/admin/monitor")
public class MonitorController {


    private final TokenService tokenService;
    private final IUserOnlineService userOnlineService;

    @GetMapping("qiniu/token")
    public ResultResponse qiniuToken(){
        String token = QiniuUtils.getUpToken(true);
        return ResultResponse.buildSuccess(token);

    }


    @GetMapping("online/list")
    @ApiOperation(value = "在线用户查询", notes = "测试Post接口")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "userName", value = "用户名", required = false, dataTypeClass = String.class),
            @ApiImplicitParam(name = "ipAdders", value = "登录地址", required = false, dataTypeClass = String.class)
    })
    public ResultResponse online(String userName, String ipAdders) {

        IPage<SysUserOnline> userOnlineList = userOnlineService.getOnline(userName, ipAdders);
        return ResultResponse.buildSuccess(userOnlineList);
    }


    @ApiOperation("强退用户")
    @Log(title = "强退在线用户", businessType = BusinessType.FORCE)
    @DeleteMapping("online/{tokenIds}")
    public ResultResponse forceLogout(@PathVariable String[] tokenIds) {
        for (String tokenId : tokenIds) {
            tokenService.logout(tokenId);
        }
        return ResultResponse.success("强退成功");
    }

    @ApiOperation("清空多端登录用户")
    @Log(title = "清空多端登录用户", businessType = BusinessType.CLEAN)
    @DeleteMapping("clean")
    public ResultResponse clean() {
        return ResultResponse.success("清空成功总共清空" + userOnlineService.clean() + "个用户");
    }

}
